

#include "fifo.h"
#include "my_macros.h"
#include "Debug.h"


void Fifo_Init(FIFO_INF* fi, unsigned char* FifoBuffer, unsigned char BufferSize)
{
	fi->pWrite = 0;
	fi->pRead = 0;
	fi->BufferSize = BufferSize;
	fi->FifoBuffer = FifoBuffer;
//	fi->AvailabelEntryCount = 0;
}

unsigned char Fifo_AvailableToRead(FIFO_INF* fi)
{
	return ((fi->BufferSize + fi->pWrite - fi->pRead) % fi->BufferSize);
}

unsigned char Fifo_AvailableToWrite(FIFO_INF* fi)
{
	return (fi->BufferSize - 1 - Fifo_AvailableToRead(fi));
}

int Fifo_Write(FIFO_INF* fi, unsigned char* DataBuffer, unsigned char ByteCount)
{
	unsigned char i = 0;

	if ( Fifo_AvailableToWrite(fi) >= ByteCount )
	{  
		for ( i = 0; i < ByteCount; i++ )
		{
			fi->FifoBuffer[fi->pWrite++] = *DataBuffer++;
		
			if ( fi->pWrite >= fi->BufferSize )
			{
				fi->pWrite = 0;
			}
		} 
	}
	else
	{
		i = (-1);
	}

	
	return i;
}


unsigned char Fifo_Read(FIFO_INF* fi, unsigned char* DataBuffer)
{
	unsigned char i = 0;
	
	for(;;)
	{
		if ( fi->pRead == fi->pWrite )
		{
			break;
		}
		
		DataBuffer[i++] = fi->FifoBuffer[fi->pRead++];
		
		if ( fi->pRead >= fi->BufferSize )
		{
			fi->pRead = 0;
		}
	}
	
	return i;
}


unsigned char Fifo_nRead(FIFO_INF* fi, unsigned char* DataBuffer, unsigned char ByteToRead)
{
	unsigned char i = 0;
	
	for(;;)
	{
		if ( fi->pRead == fi->pWrite )
		{
			break;
		}
		
		DataBuffer[i++] = fi->FifoBuffer[fi->pRead++];
				
		if ( fi->pRead >= fi->BufferSize )
		{
			fi->pRead = 0;
		}

		if ( i >= ByteToRead )
		{
			break;
		}
	}

	return i;
}

/* FIFO ENTRY */








/* /\* in interrupt using for;  *\/ */
/* int Fifo_Entry_i_Puts(FIFO_INF* fi, unsigned char* DataBuffer, unsigned int ByteCount) */
/* { */
/* 	int i; // , ByteCount; */
	
/* /\* 	ByteCount = StrLen((const char*)DataBuffer); *\/ */
/* /\* 	ByteCount++;					  /\\* place for zero *\\/ *\/ */

/* 	if ( Fifo_AvailableToWrite(fi) >= ByteCount ) */
/* 	{   */
/* 		for ( i = 0; i < ByteCount; i++ ) */
/* 		{ */
/* 			fi->FifoBuffer[fi->pWrite++] = *DataBuffer++; */
		
/* 			if ( fi->pWrite >= fi->BufferSize ) */
/* 			{ */
/* 				fi->pWrite = 0; */
/* 			} */
/* 		} */
/* 		fi->AvailabelEntryCount++; */
/* 	} */
/* 	else */
/* 	{ */
/* 		i = (-1); */
/* 	} */

/* 	return i; */
/* } */

/* int Fifo_Entry_i_PutCh(FIFO_INF* fi, unsigned char Ch) */
/* { */
/* 	int i; */
	
/* 	if ( Fifo_AvailableToWrite(fi) >= 1 ) */
/* 	{   */
/* 		fi->FifoBuffer[fi->pWrite++] = Ch; */
		
/* 		if ( fi->pWrite >= fi->BufferSize ) */
/* 		{ */
/* 			fi->pWrite = 0; */
/* 		} */
/* 	} */
/* 	else */
/* 	{ */
/* 		i = (-1); */
/* 	} */

/* 	return i; */
/* } */

/* int Fifo_Entry_Read(FIFO_INF* fi, unsigned char* DataBuffer) */
/* { */
/* 	int i = 0; */
	
/* 	int save_status_reg = 0; */
/* 	save_status_reg = cpu_save_sr(); */

/* 	if ( fi->AvailabelEntryCount > 0 ) */
/* 	{ */
/* 		for(;;) */
/* 		{ */
/* /\* 			if ( fi->pRead == fi->pWrite ) *\/ */
/* /\* 			{ *\/ */
/* /\* 				DBG_OUT("\r ERROR: Fifo_Entry_Read\r"); *\/ */
/* /\* 				break; *\/ */
/* /\* 			} *\/ */
		
/* 			DataBuffer[i++] = fi->FifoBuffer[fi->pRead++]; */
					
/* 			if ( fi->pRead >= fi->BufferSize ) */
/* 			{ */
/* 				fi->pRead = 0; */
/* 			} */

/* 			if ( DataBuffer[i-1] == 0 ) /\* end of entry (string) *\/ */
/* 			{ */
/* 				fi->AvailabelEntryCount--; */
/* 				break; */
/* 			} */
/* 		} */
/* 	} */
	
/* 	cpu_restore_sr(save_status_reg); */

/* 	return i; */
/* } */

/* /\* return - pointer at the available Entry if MessageStatus = 1, do the Entry as read *\/ */
/* unsigned char* Fifo_Entry_Get(FIFO_INF* fi, int MessageStatus) */
/* { */
/* 	int tmp; */
/* 	unsigned char* p; */

/* 	p = NULL; */

/* 	int save_status_reg = 0; */
/* 	save_status_reg = cpu_save_sr(); */

  
/* 	if ( fi->AvailabelEntryCount > 0 ) */
/* 	{ */
/* 		if ( MessageStatus == ENTRY_MESSAGE_READ ) */
/* 		{ */
/* 		   p = &(fi->FifoBuffer[fi->pRead]);  */
/* 		} */
/* 		else */
/* 		{ */
/* 			for(;;) */
/* 			{ */
/* 				tmp = fi->FifoBuffer[fi->pRead++]; */
				
/* 				if ( fi->pRead >= fi->BufferSize ) */
/* 				{ */
/* 					fi->pRead = 0; */
/* 				} */
	
/* 				if ( tmp == 0 ) /\* end of entry (string) *\/ */
/* 				{ */
/* 					fi->AvailabelEntryCount--; */
/* 					break; */
/* 				} */
/* 			} */
/* 			if ( fi->AvailabelEntryCount > 0 ) */
/* 			{ */
/* 				p = &(fi->FifoBuffer[fi->pRead]);  */
/* 			} */
/* 		} */
/* 	} */
	
/* 	cpu_restore_sr(save_status_reg); */

/* 	return p; */
/* } */


/* int Fifo_i_Write(FIFO_INF* fi, unsigned char* DataBuffer, unsigned char ByteCount) */
/* { */
/* 	int i = 0; */
	
/* 	if ( Fifo_AvailableToWrite(fi) >= ByteCount ) */
/* 	{   */
/* 		for ( i = 0; i < ByteCount; i++ ) */
/* 		{ */
/* 			fi->FifoBuffer[fi->pWrite++] = *DataBuffer++; */
		
/* 			if ( fi->pWrite >= fi->BufferSize ) */
/* 			{ */
/* 				fi->pWrite = 0; */
/* 			} */
/* 		}  */
/* 	} */
/* 	else */
/* 	{ */
/* 		i = (-1); */
/* 	} */
	
/* 	return i; */
/* } */
